//
//  PaymenHistoryViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/11/8.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "PaymenHistoryViewController.h"
#import "TransactionDetailsViewController.h"
#import "MallRequestManager.h"
#import "PaymentHistoryCell.h"
#import "CLUIBaseTableViewController+Empty.h"

@interface PaymenHistoryViewController ()
@property (nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation PaymenHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"续费缴费历史";
    [self showNavgationWithLeftItemWithTitle:@"返回"];
    [self.tableView registerNib:[UINib nibWithNibName:@"PaymentHistoryCell" bundle:nil] forCellReuseIdentifier:@"PaymentHistoryCell"];
    [self requestData];
}
- (void)requestData
{
    NSMutableDictionary *params = [NSMutableDictionary new];
    //如果用户没有登陆则用户编号为0
    NSString *userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userID"];
    int num = [userId intValue];
    NSNumber *userIdNum = [NSNumber numberWithInt:num];
    [params setObject:userIdNum forKey:@"user_id"];
    [SVProgressHUD showWithStatus:@"加载中..."];
    [MallRequestManager post:@"Charge/feehistory" RequestParams:params FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [SVProgressHUD dismiss];
        if (connectionError) {
            [self performSelectorOnMainThread:@selector(errorAction) withObject:nil waitUntilDone:YES];
        }else{
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            if (dic!=nil) {
                if ([dic.allKeys containsObject:@"result"]) {
                    self.dataSource = [NSMutableArray new];
                    NSDictionary *result = [dic objectForKey:@"result"];
                    for (NSDictionary *tmpDic in result) {
                        [self.dataSource addObject:tmpDic];
                    }
                }
            }
            [self performSelectorOnMainThread:@selector(reloadUI) withObject:nil waitUntilDone:YES];
        }
    }];
}

- (void)reloadUI{
    if (self.dataSource.count == 0) {
        [self showLabelEmptyCate:CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT -140 -64)/2, 100, 100) withTitle:@"这里还没有内容" withImage:[UIImage imageNamed:@"icon_ blank"]];
    }
    [self.tableView reloadData];
}

- (void)errorAction{
    [self showLabelEmptyCate:CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT -140 -64)/2, 100, 100) withTitle:@"出错了，稍后再试吧" withImage:[UIImage imageNamed:@"icon_wifi"]];

}

#pragma  mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"PaymentHistoryCell";
    PaymentHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[PaymentHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
    }
    
    NSDictionary *dic = [self.dataSource objectAtIndex:indexPath.section];
    if ([[dic objectForKey:@"type"]isEqualToString:@"1"]) {
        [cell.imgView setImage:[UIImage imageNamed:@"服务费续费"]];
    }else
    {
        [cell.imgView setImage:[UIImage imageNamed:@"漫游费缴纳"]];
    }
    cell.moneyLabel.text = [NSString stringWithFormat:@"￥%@",[dic objectForKey:@"payamount"]];
    cell.dateLabel.text = [dic objectForKey:@"paytime"];
    cell.titleLabel.text = [dic objectForKey:@"title"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
#pragma mark - UITableview Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TransactionDetailsViewController *transaction = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"TransactionDetailsViewController"];
     NSDictionary *dic = [self.dataSource objectAtIndex:indexPath.section];
    transaction.orderId  = [dic objectForKey:@"id"];
    [self.navigationController pushViewController:transaction animated:YES];
}
@end
